
var layer = layui.layer, //弹层
    table = layui.table, //表格
    form = layui.form,
    $ = layui.jquery,
    laydate = layui.laydate,
    currencyCode = getQueryVariable("currencyCode");
lay('.starttime').each(function(){
    laydate.render({
        elem: this
    });
});

layer.ready(function () {
    $.ajax({
        url:"otherOutStock/getAllUserInfo",
        success:function(data){
            if(data.status == 200){
                for(let i in data.data){
                    $('#salesmanCode').append(new Option(data.data[i].name, data.data[i].code));
                }
                $('#salesmanCode').val(user.code);
                form.render('select');
            }
        }
    });
});
let importContractData = [];
table.render({
    elem: '#mains'
    ,data:importContractData
    ,cols: [[
        {checkbox: true, sort : true,title:'选择'}
        ,{field:'contractNo', title:'合同编号',sort:true}
        ,{field:'claimCustomer', title:'客户名称',sort:true}
        ,{field:'customerContractNo', title:'客户合同号',sort:true}
        ,{field:'claimSalesman', title:'业务员',sort:true}
        ,{field:'operator', title:'操作员',sort:true}
        ,{field:'totalContract', title:'合同总金额',sort:true}
        ,{field:'advanceCollectionRatio', title:'预收款比例',sort:true}
        ,{field:'advanceReceiptAmount', title:'预收款金额',sort:true}
        ,{field:'amountReceived', title:'已收款金额',sort:true}
    ]]
    ,id: 'mainstableid'
    ,height: 400
    , parseData: function(res){
        return {
            "code": res.status,
            "count": res.total,
            "data": res.data
        };
    }
});

form.on('submit(submitform)', function(data){
    data.field.currencyCode=currencyCode
    table.reload('mainstableid', {
        where: data.field,
        url:"receivePayment/getImportContractInfoByNotPage"
    }, 'data');
    return false;
});

$("#choose").on("click",function () {
    //第一个表格选中的数据
    let productData = table.checkStatus("mainstableid").data;
    if(productData.length == 0){
        layer.msg("请选择数据")
    }else{
        //获取父页面明细数据
        let oldData =  parent.table.cache["claimdetailtableid"];
        let num = oldData.length+1;
        $.each(productData, function (index, value) {
            //剔除重复数据
            let flag = true;
            if(oldData.length > 0){
                for(var i = 0; i < oldData.length; i++){
                    if(oldData[i].contractId == productData[index].contractId){
                        flag = false;
                        break;
                    }
                }
            }
            if(flag){
                //不存在重复数据,添加数据
                //添加临时id
                productData[index].tempId = GenNonDuplicateID(3) + num;
                productData[index].foreignBankDeductions = 0;
                //已确认默认为0
                productData[index].confirmed = 0;

                let advanceReceiptAmount = productData[index].advanceReceiptAmount;
                if(new BigNumber(advanceReceiptAmount).comparedTo(new BigNumber(0)) == 1){
                    //关联合同时,如果有预收款,那么收款模块的 收汇金额就是预收款金额
                    productData[index].collectionAmount = advanceReceiptAmount;
                    //认领金额
                    productData[index].amountClaimed = advanceReceiptAmount;
                    //未分摊金额
                    productData[index].unallocatedAmount = advanceReceiptAmount;
                }else {
                    //如果没有比例,那么收汇金额=合同金额-已收款金额

                    //合同金额
                    let totalContract = productData[index].totalContract;
                    if(!checkNumber(totalContract)){
                        totalContract = 0;
                    }
                    //已收款金额
                    let amountReceived = productData[index].amountReceived;
                    if(!checkNumber(amountReceived)){
                        amountReceived = 0;
                    }

                    let collectionAmount = new BigNumber(totalContract).minus(amountReceived).dp(4, BigNumber.ROUND_HALF_UP).toString();
                    productData[index].collectionAmount = collectionAmount;
                    //认领金额
                    productData[index].amountClaimed = collectionAmount;
                    //未分摊金额
                    productData[index].unallocatedAmount = collectionAmount;
                }
                //个别字段初始值为0
                oldData.push(productData[index]);
                num++;
            }
        });

        parent.table.reload('claimdetailtableid',{data : oldData, limit: oldData.length});
        //执行接口
        layer.msg("保存成功", {icon: 6});
        setTimeout(function () {
            //保存成功则关闭此页面
            let index = parent.layer.getFrameIndex(window.name);
            parent.layer.close(index);
        },)
    }
})
$("#shutdown").on("click",function () {
    let index = parent.layer.getFrameIndex(window.name);
    parent.layer.close(index);
})

//生成临时id
function GenNonDuplicateID(randomLength){
    return Number(Math.random().toString().substr(3,randomLength) + Date.now()).toString(36)
}

function checkNumber(val) {
    if(isEmpty(val)){
        return false;
    }
    if(!isNumber(val)){
        return false;
    }
    return true;
}

//字段是否为空或者零
function isEmpty(obj){
    if(typeof obj == "undefined" || obj == null || obj == "" || obj == 0){
        return true;
    }else{
        return false;
    }
}

function isNumber(val) {
    var regPos = /^\d+(\.\d+)?$/; //非负浮点数
    var regNeg = /^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$/; //负浮点数
    if (regPos.test(val) || regNeg.test(val)) {
        return true;
    } else {
        return false;
    }
}